<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    //安全模式创建的工厂方法函数
    let UserFactory = function (role) {
      if (this instanceof UserFactory) {
        console.log(this);
        var s = new this[role]();
        return s;
      } else {
        console.log(this);
        return new UserFactory(role);
      }
    }

    //工厂方法函数的原型中设置所有对象的构造函数
    UserFactory.prototype = {
      SuperAdmin: function () {
        this.name = "超级管理员",
          this.viewPage = ['首页', '通讯录', '发现页', '应用数据', '权限管理']
      },
      Admin: function () {
        this.name = "管理员",
          this.viewPage = ['首页', '通讯录', '发现页', '应用数据']
      },
      NormalUser: function () {
        this.name = '普通用户',
          this.viewPage = ['首页', '通讯录', '发现页']
      }
    }

    //调用
    let superAdmin = UserFactory('SuperAdmin'); // 首先执行 new UserFactory('SuperAdmin')
    console.log(superAdmin);
    // let admin = UserFactory('Admin')
    // let normalUser = UserFactory('NormalUser')
  </script>
</body>

</html>